<?php
/**
 * Created by PhpStorm.
 * User: renyi
 * Date: 2017/3/4
 * Time: 上午11:44
 */

namespace App\Base;

class CommonPool
{
    // --------------------------------------------------------------------------------------------
    // 单例模式
    // --------------------------------------------------------------------------------------------

    // 单例
    private static $_instance;

    public static function instance()
    {
        if (empty(self::$_instance))
        {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    // --------------------------------------------------------------------------------------------
    // 基础函数
    // --------------------------------------------------------------------------------------------

    // 功能池
    private $pool = array();

    /**
     * 基础类 创建Service 服务
     * @param $name
     * @return mixed
     */
    public function createService($name)
    {
        $key = $this->getPoolKey("service", $name);

        if (empty($this->pool[$key]))
        {
            $class = $this->getClassName("service", $name);
            if (class_exists($class))
            {
                $this->pool[$key] = new $class();
            }
            else
            {
                return null;
            }
        }

        return $this->pool[$key];
    }

    /**
     * 基础类 创建Repo 服务
     * @param $name
     * @return mixed
     */
    public function createRepository($name)
    {
        $key = $this->getPoolKey("repo", $name);

        if (empty($this->pool[$key]))
        {
            $class = $this->getClassName("repo", $name);
            if (class_exists($class))
            {
                $this->pool[$key] = new $class();
            }
            else
            {
                return null;
            }
        }

        return $this->pool[$key];
    }

    /**
     * 基础类 创建 Talk 对话
     * @param $name
     * @return mixed
     */
    public function createTalk($name)
    {
        $key = $this->getPoolKey("talk", $name);

        if (empty($this->pool[$key]))
        {
            $class = $this->getClassName("talk", $name);
            if (class_exists($class))
            {
                $this->pool[$key] = new $class();
            }
            else
            {
                return null;
            }

        }

        return $this->pool[$key];
    }

    /**
     * 基础类 创建 Talk 对话
     * @param $name
     * @return mixed
     */
    public function createModel($name)
    {
        $key = $this->getPoolKey("model", $name);

        if (empty($this->pool[$key]))
        {
            $class = $this->getClassName("model", $name);
            if (class_exists($class))
            {
                $this->pool[$key] = new $class();
            }
            else
            {
                return null;
            }

        }

        return $this->pool[$key];
    }

    /**
     * 获得CommonPool的key的名称
     *
     * @param $type
     * @param $name
     * @return string
     */
    private function getPoolKey($type, $name)
    {
        if ($type == "service")
        {
            return $name."_service";
        }
        else if ($type == "repo")
        {
            return $name."_repository";
        }
        else if ($type == "talk")
        {
            return $name."_talk";
        }
        else if ($type == "model")
        {
            return $name."_model";
        }
        else
        {
            return '';
        }
    }

    /**
     * 获得类的路径
     * @param $type
     * @param $name
     * @return string
     */
    private function getClassName($type, $name)
    {
        $name = ucfirst($name);
        if ($type == "service")
        {
            return "\\App\\Service\\{$name}Service";
        }
        else if ($type == "model")
        {
            return "\\App\\Models\\{$name}";
        }
        else if ($type == "repo")
        {
            return "\\App\\Repository\\{$name}Repository";
        }
        else
        {
            return '';
        }

    }

    // --------------------------------------------------------------------------------------------
    // 基础存储
    // --------------------------------------------------------------------------------------------
    private $params = array();

    public function saveParams($key, $param)
    {
        //Log::info("CommonPool saveParams key=[$key]");
        $this->params[$key] = $param;
    }

    public function getParams($key)
    {
        //Log::info("CommonPool getParams key=[$key]");
        if (array_key_exists($key, $this->params))
        {
            return $this->params[$key];
        }

        return null;
    }

    public function removeParams($key)
    {
        if (array_key_exists($key, $this->params))
        {
            unset($this->params[$key]);
        }
    }
}
